from alive_progress import alive_bar
from time import sleep
from re import search
from winsound import Beep

'''会导致无法输入
import ctypes  # 这一行还有以下几行是为了防止程序被点击后暂停

kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), 128)
'''


def main(work_time=25, break_time=5):
    print(f"开始番茄钟，工作{work_time}分钟，休息{break_time}分钟")
    total_work = work_time * 60
    total_break = break_time * 60
    with alive_bar(total_work, title="work") as bar:
        for i in range(total_work):
            sleep(1)
            bar()
        print("工作时间结束")
        Beep(2000, 440)
    with alive_bar(total_break, title="rest") as bar:
        for i in range(total_break):
            sleep(1)
            bar()
        print("休息时间结束，如果要进入下一轮请按回车或者重新输入数字-数字")
        Beep(2000, 440)


if __name__ == "__main__":
    print("直接按下回车执行标准番茄钟，输入 数字1-数字2 后回车可以激活一个工作数字1时间，休息数字2时间的番茄钟\n按下Ctrl+C可以退出程序")
    while True:
        msg = input()
        if msg == "":
            main()
        elif search(r"\d-\d", msg) != None:
            try:
                work_time = int(msg.split("-")[0])
                break_time = int(msg.split("-")[1])
                main(work_time, break_time)
            except TypeError:
                print("请输入整数，，重新输入")
        else:
            print("输入错误，重新输入")
